<cpanel include="../../config.php">

<?

/***************************************************************************
 *                                 mysql_main.php
 *                            -------------------
 *   Begin                : Thursday, April 16, 2005
 *   Copyright            : (C) 2005 Point and Code
 *   Domain               : http://www.pointandcode.com/
 *   Email                : support@pointandcode.com
 *   Version              : 1.0.0
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

$html_root_path = '../../';
include $root_path.'common.php';
$page_title = $lang['mysql_databases'];
include $root_path.'page_header.php';





$big_wadge_of_stuff = <<<EOFpacman
<cpanel Mysql="listdbs()">
EOFpacman;



$big_wadge_of_stuff = str_replace("</blockquote>\n</blockquote>", "</blockquote></blockquote>", $big_wadge_of_stuff);

$array_of_databases = explode("</blockquote></blockquote><br>", $big_wadge_of_stuff);

$array_split_into_users = array();

$i=0;
foreach($array_of_databases as $row) {

	// to fix a bug with dbs with no users...
	
	if (strpos($row, 'Users in') == 0) {
	
		$array_split_into_users[$i]['database_info'] = $row;
		$array_split_into_users[$i]['users'] = array();

	
	} else {


	
		$first_user = strpos($row, '</u><br>');
		$first_user = $first_user + 8;
		$database_info = substr($row, 0, $first_user);
	
		$array_split_into_users[$i]['database_info'] = $database_info;
		
		$rest = substr($row, $first_user);
		
		$array_of_users = explode("</blockquote>\n", $rest);
		
		$j=0;
		foreach($array_of_users as $user) {
		
			$array_split_into_users[$i]['users'][$j] = $user;	
			
			$j++;
			
		}
	}	
	$i++;
}

//
// Done the splitting, now let's extract the usable data from the wadge
//

$usable_data = array();

$i=0;
foreach($array_split_into_users as $database) {

	$temp = explode(' ', $database['database_info']);
	
	$database_name = $temp[0];
	$database_name = str_replace('<b>', '', $database_name);
	$database_name = str_replace('</b>', '', $database_name);
	
	$usable_data[$i]['name'] = $database_name;
	
	$usable_data[$i]['users'] = array();
	
	$j=0;
	foreach($database['users'] as $user) {
		$temp = explode(' ', $user);
		$usable_data[$i]['users'][$j]['name'] = $temp[0];
		
		$first_bracket = strpos($user, '(');
		$second_bracket = strpos($user, ')');
		
		$privs = substr($user, ($first_bracket + 13), ($second_bracket - $first_bracket - 13));
		
		$usable_data[$i]['users'][$j]['privs'] = $privs;
		$j++;
	}
	$i++;
}


$template->set_filenames(array(
	'body' => $root_path.$template_path.'databases/mysql_main.tpl')
);

$i = 0;
foreach ($usable_data as $row) {

	if (!empty($row['name'])) {
		$template->assign_block_vars('databaserow', array(
			'ROWCLASS' => ($i%2) ? $template_cfg['class_row_1'] : $template_cfg['class_row_2'],
			'NAME' => $row['name'],
			'U_DELETE' => 'mysql_delete.php?db='.$row['name'],
			'U_CHECK' => 'mysql_check_done.php?db='.$row['name'],
			'U_REPAIR' => 'mysql_repair_done.php?db='.$row['name'],			
			)
		);
		
		foreach($row['users'] as $user) {
			$template->assign_block_vars('databaserow.userrow', array(
				'NAME' => $user['name'],
				'PRIVS' => $user['privs'],
				'U_DELETE' => 'mysql_user_unlink.php?db='.$row['name'].'&user='.$user['name'],
				)
			);
		}
	$i++;
	}	
}

//
// THAT'S ALL FOLKS!
// For the list of databases and active users, anyhow.
//

//
// Global page variables
//
$template->assign_vars(array(
	'S_CHOOSE_DATABASE' => '<select name=db><cpanel Mysql="listdbsopt()"></select>',
	'S_CHOOSE_USER' => '<select name=user><cpanel Mysql="listusersopt()"></select>',
	'S_ADD_USER_TO_DB_ACTION' => 'mysql_user_link_done.php',
	
	'S_ADD_DB_ACTION' => 'mysql_db_add_done.php',
	
	'S_ADD_USER_ACTION' => 'mysql_user_add_done.php',
	
	'S_ADD_ACCESS_HOST_ACTION' => 'mysql_host_add_done.php',
	
	'U_PHPMYADMIN' => '<cpanel Cgi="phpmyadminlink())">',
	
	'L_MYSQL_DATABASES' => $lang['mysql_databases'],
	'L_MYSQL_INTRO' => $lang['mysql_intro'],
	'L_PHPMYADMIN_INTRO' => $lang['phpmyadmin_intro'],
	'L_DATABASE_NAME' => $lang['database_name'],
	'L_DATABASE_ACTIONS' => $lang['database_actions'],
	'L_DATABASE_USERS' => $lang['database_users'],
	
	'L_DELETE' => $lang['delete'],
	'L_CHECK' => $lang['check'],
	'L_REPAIR' => $lang['repair'],
	'L_REMOVE' => $lang['remove'],
	
	'L_LINK_USER_DATABASE' => $lang['link_user_database'],
	'L_LINK_USER_DATABASE_INTRO' => $lang['link_user_database_intro'],
	
	'L_USER' => $lang['user'],
	'L_DATABASE' => $lang['database'],
	'L_PRIVILEGES' => $lang['privileges'],
	
	'L_ALL' => $lang['all'],
	'L_ALTER' => $lang['alter'],
	'L_CREATE_TEMPORARY_TABLES' => $lang['create_temporary_tables'],
	'L_CREATE' => $lang['create'],
	'L_DROP' => $lang['drop'],
	'L_SELECT' => $lang['select'],
	'L_INSERT' => $lang['insert'],
	'L_UPDATE' => $lang['update'],
	'L_REFERENCES' => $lang['references'],
	'L_INDEX' => $lang['index'],
	'L_LOCK_TABLES' => $lang['lock_tables'],
	
	'L_CREATE_DATABASE' => $lang['create_database'],
	
	'L_USERS' => $lang['users'],
	'L_DATABASE_USERS_INTRO' => $lang['database_users_intro'],
	'L_USERNAME' => $lang['username'],
	'L_ACTIONS' => $lang['actions'],
	'L_PASSWORD' => $lang['password'],
	'L_ADD_USER' => $lang['add_user'],
	
	'L_ACCESS_HOSTS' => $lang['access_hosts'],
	'L_ACCESS_HOSTS_INTRO' => $lang['access_hosts_intro'],
	'L_ADDRESS' => $lang['address'],
	'L_HOST' => $lang['host'],
	'L_ADD_HOST' => $lang['add_host'],
	)
);

//
// Parse the listing of currently available users
//


$users_with_junk = <<<EOF
<cpanel Mysql="listusers()">
EOF;

$users_in_array_with_junk = explode('<br>', $users_with_junk);

$i=0;
$users_in_array = array();

foreach($users_in_array_with_junk as $row) {
	
	$temp = explode(' ', $row);
	$users_in_array[$i] = $temp[0];
	$i++;

}

$i = 0;
foreach ($users_in_array as $row) {

	if (!empty($row)) {
		$template->assign_block_vars('listuserrow', array(
			'ROWCLASS' => ($i%2) ? $template_cfg['class_row_1'] : $template_cfg['class_row_2'],
			'NAME' => $row,
			'U_DELETE' => 'mysql_user_delete.php?user='.$row,
			)
		);
	$i++;
	}	
}


//
// Let's do the access hosts
//

$hosts_with_junk = <<<EOF
<cpanel Mysql="listhosts()">
EOF;


$hosts_in_array_with_junk = explode('<br>', $hosts_with_junk);

$i=0;
$hosts_in_array = array();

foreach($hosts_in_array_with_junk as $row) {
	
	$temp = explode(' ', $row);
	$hosts_in_array[$i] = $temp[0];
	$i++;

}

$i = 0;
foreach ($hosts_in_array as $row) {

	if (!empty($row)) {
		$template->assign_block_vars('listhostrow', array(
			'ROWCLASS' => ($i%2) ? $template_cfg['class_row_1'] : $template_cfg['class_row_2'],
			'NAME' => $row,
			)
		);
		if ($row != 'localhost') {
			$template->assign_block_vars('listhostrow.showdelete', array(
				'U_DELETE' => 'mysql_host_delete.php?host='.$row,
				)
			);
		}
	$i++;
	}	
}



//
// Woohoo! All done!
//
$template->pparse('body');




include $root_path.'page_tail.php';




?>